<?php
class Default_ArchitectController extends Zend_Controller_Action {
	public function init() {
		/* Initialize action controller here */
		$this->_model = new Default_Model_DbTable_Object ();
	}
	public function indexAction() {
		// action body
		$page = ($this->getRequest ()->getParam ( 'page' ) != null) ? $this->getRequest ()->getParam ( 'page' ) : 1;
		// set rows per page
		$rp = 8;
		$total = $this->_model->getRowCount ( 'Kiến trúc sư' );
		$limit = 5;
		
		$architects = $this->_model->ShowAllArchitect ( $rp, $rp * ($page - 1), 'Kiến trúc sư' );
		$this->view->allArc = $architects;
		$this->view->pg = $this->paging ( $page, $rp, $total, $limit );
	}
	public function detailAction() {
		$objPhoto = new Default_Model_DbTable_ObjectPhotoProfile ();
		$libPhoto = new Default_Model_DbTable_ObjectLibPhoto ();
		$promotion = new Default_Model_DbTable_ObjectDetail ();
		$objectModel = new Default_Model_DbTable_Object ();
		$promotionModel = new Default_Model_DbTable_ObjectPromotion ();
		
		$id = $this->_getParam ( "ID" );
		
		if ($id == null || $id == 0) {
		} else {
			$photo = $objPhoto->getObjectPhotoProfileByObjectId ( $id );
			$this->view->photoProfile = $photo;
			
			$promo = $promotionModel->getPromotionByOjectId ( $id );
			$this->view->promotion = $promo;
			
			$object = $objectModel->getObjectById ( $id );
			$this->view->objectInfo = $object;
			
			$libProductWithPosition1 = $libPhoto->getObjectLibPhotoByObjectId ( $id, 1 );
			$this->view->libProject1 = $libProductWithPosition1;
			
			$libProductWithPosition2 = $libPhoto->getObjectLibPhotoByObjectId ( $id, 2 );
			$this->view->libProject2 = $libProductWithPosition2;
			
			$this->view->id = $id;
		}
	}
	public function showlibphotoAction() {
		$modelPhoto = new Default_Model_DbTable_ObjectPhotoProfile ();
		$id = $this->getRequest ()->getParam ( "ID" );
		$this->view->test = $id;
		
		if ($id == null || $id == 0) {
		} else {
			$allPhotoByLibPhoto = $modelPhoto->getObjectPhotoProfileByLibPhotoId ( $id );
			$this->view->allPhotoByLibPhoto = $allPhotoByLibPhoto;
		}
		
		$this->_helper->layout ()->disableLayout ();
	}
	public function showlibphoto2Action() {
		$objPhoto = new Default_Model_DbTable_ObjectPhotoProfile ();
		$id = $this->getRequest ()->getParam ( "ID" );
		
		if ($id == 0 || $id == null) {
		} else {
			$photo = $objPhoto->getObjectPhotoProfileByObjectId ( $id );
			$this->view->photo = $photo;
		}
		$this->_helper->layout ()->disableLayout ();
	}
	public function showlibphoto3Action() {
		$id = $this->getRequest ()->getParam ( "ID" );
		$this->view->id = $id;
		$this->_helper->layout ()->disableLayout ();
	}
	public function popupAction() {
		$objPhoto = new Default_Model_DbTable_ObjectPhotoProfile ();
		$libPhoto = new Default_Model_DbTable_ObjectLibPhoto ();
		$promotion = new Default_Model_DbTable_ObjectDetail ();
		$objectModel = new Default_Model_DbTable_Object ();
		$id = $this->getRequest ()->getParam ( "ID" );
		if ($id == null || $id == 0) {
		} else {
			$object = $objectModel->getObjectById ( $id );
			$this->view->objectInfo = $object;
		}
		$this->_helper->layout ()->disableLayout ();
	}
	function paging($page, $rp, $total, $limit) {
		$limit -= 1;
		
		$mid = floor ( $limit / 2 );
		
		if ($total > $rp)
			$numpages = ceil ( $total / $rp );
		else
			$numpages = 1;
		
		if ($page > $numpages)
			$page = $numpages;
		
		$npage = $page;
		
		while ( ($npage - 1) > 0 && $npage > ($page - $mid) && ($npage > 0) )
			$npage -= 1;
		
		$lastpage = $npage + $limit;
		
		if ($lastpage > $numpages) {
			$npage = $numpages - $limit + 1;
			if ($npage < 0)
				$npage = 1;
			$lastpage = $npage + $limit;
			if ($lastpage > $numpages)
				$lastpage = $numpages;
		}
		
		while ( ($lastpage - $npage) < $limit )
			$npage -= 1;
		
		if ($npage < 1)
			$npage = 1;
			
			// echo $npage; exit;
		
		$paging ['first'] = 1;
		if ($page > 1)
			$paging ['prev'] = $page - 1;
		else
			$paging ['prev'] = 1;
		$paging ['start'] = $npage;
		$paging ['end'] = $lastpage;
		$paging ['page'] = $page;
		if (($page + 1) < $numpages)
			$paging ['next'] = $page + 1;
		else
			$paging ['next'] = $numpages;
		$paging ['last'] = $numpages;
		$paging ['total'] = $total;
		$paging ['iend'] = $page * $rp;
		$paging ['istart'] = ($page * $rp) - $rp + 1;
		
		if (($page * $rp) > $total)
			$paging ['iend'] = $total;
		
		return $paging;
	}
}

